import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  userLists: [],
  userFields: [],
  Pages: {},
  passportAllData: [],
  setuserSupUserData: [],
  indexPageData: {},
  loginMsg: {},
};
const actions = {
  // 登录信息
  getloginMsg({ commit }) {
    request.get(Server.action.loginLoginNum).then((response) => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit('setLoginMsg', response.data.result);
      }
    });
  },
  // 列表
  userLists({ commit }, page) {
    request.get(Server.action.passport_list, { params: page })
      .then((response) => {
        if (response.data.errorCode === 0) {
          commit('setUserLists', response.data.result.list);
          // commit("setPage", response.data.result.list);
          const { fields } = response.data.result;
          fields.push({
            title: '操作',
            align: 'center',
            width: 140,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          });
          fields.forEach((item, i) => {
            item.dataIndex = item.key;
            item.align = 'center';
            if (i < fields.length - 2) {
              item.width = item.width || 120;
            }
            if (item.key == 'activation_status') {
              item.width = 80;
              item.scopedSlots = { customRender: 'activation_status' };
            }
          });
          fields[0].fixed = 'left';
          fields[0].width = 50;
          // fields[1].fixed = 'left'
          fields[1].width = 100;
          // fields[fields.length-2].width = 200
          commit('setFields', fields);
        } else {
        }
      });
  },
  // 添加
  addUser({ commit }, page) {
    commit('setErrorCode', false);
    if (page.name == '' && page.name == '') {
      showError('用户姓名不能为空');
      showError('手机号码不能为空');
      return null;
    }
    if (page.name == '') {
      showError('用户姓名不能为空');
      return null;
    }
    if (page.phone == '') {
      showError('手机号码不能为空');
      return null;
    }

    request.post(Server.action.passport_register, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else if (response.status === 422) {
          showErrors(response.data.errors);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 编辑
  editUser({ commit }, page) {
    commit('setErrorCode', false);
    if (page.name == '' && page.name == '') {
      showError('用户姓名不能为空');
      showError('手机号码不能为空');
      return null;
    }
    if (page.name == '') {
      showError('用户姓名不能为空');
      return null;
    }
    if (page.phone == '') {
      showError('手机号码不能为空');
      return null;
    }

    request.post(Server.action.passport_update, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 修改密码
  changePass({ commit }, page) {
    commit('setErrorCode', false);
    request.post(Server.action.passport_password, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else if (response.data.errorCode === 414) {
          showErrors(response.data.message);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 解绑微信
  wxUntyingUser({ commit }, id) {
    request.get(`${Server.action.wechat_WxUntyingUser}?user_id=${id}`)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
        // showError( response.data.message )
        }
      });
  },
  // 删除
  delUser({ commit }, id) {
    request.get(Server.action.passport_delete + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
        // showError( response.data.message )
        }
      });
  },
  // 激活账号
  UserActivation({ commit }, id) {
    commit('setErrorCode', false);
    request.get(Server.action.passport_upActivationStatus + id)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
        // showError( response.data.message )
        }
      });
  },
  // 所有用户信息
  passportAll({ commit }) {
    request.get(Server.action.passport_all)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setPassportAll', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 上级用户信息
  userSupUser({ commit }, id) {
    request.get(Server.action.user_supUser + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setUserSupUser', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 上级用户信息
  indexPage({ commit }) {
    request.get(Server.action.index_page)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setIndexPage', response.data.result);
        }
      });
  },
};
const mutations = {
  setUserLists(state, lists) {
	    state.userLists = lists;
  },
  setFields(state, lists) {
	    state.userFields = lists;
  },
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setPassportAll(state, data) {
    state.passportAllData = data;
  },
  setUserSupUser(state, data) {
    state.setuserSupUserData = data;
  },
  setIndexPage(state, data) {
    state.indexPageData = data;
  },
  setLoginMsg(state, data) {
    state.loginMsg = data;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
